package com.example.test;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.util.Log;
/**
 * @author lamtt
 * create: 07/02/2013
 * BaseDatabase class for conect database
 */
public class DataContentProvider extends ContentProvider{
	////////////////////////////////////////////////////////////////////////////
	//  private static
	////////////////////////////////////////////////////////////////////////////
//	private static UriMatcher mUriMatcher = null;
	private SQLiteDatabase mSqlDb; 
	private static final int VOCABULARY_TABLE = 1;
    private static final String TITLE_ERROR_EXCEPTION = "LamTT --> Class: DataContentProvider - function: ";
    private static  UriMatcher mURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    
	////////////////////////////////////////////////////////////////////////////
	//  public method
	////////////////////////////////////////////////////////////////////////////
    static {
      mURIMatcher.addURI(ConfigLib.PROVIDER_AUTHORITY, Vocabulary.TABLE_NAME, VOCABULARY_TABLE);
    }

	/* (non-Javadoc)
	 * @see android.content.ContentProvider#delete(android.net.Uri, java.lang.String, java.lang.String[])
	 */
	@Override
	public int delete(Uri uri, String whereClause, String[] whereArgs) {
		return 1;
	}

	
	/* (non-Javadoc)
	 * @see android.content.ContentProvider#getType(android.net.Uri)
	 */
	@Override
	public String getType(Uri uri) {

		 switch (mURIMatcher.match(uri)) {
	        case VOCABULARY_TABLE:
	            return Vocabulary.TABLE_NAME;
            default:
                break;
	        }
	        return null;
	}

	/* (non-Javadoc)
	 * @see android.content.ContentProvider#insert(android.net.Uri, android.content.ContentValues)
	 */
	@Override
	public Uri insert(Uri uri, ContentValues values) {
		String tableName = getType(uri);
		long rowID = mSqlDb.insert(tableName, "", values);
		if(rowID > 0)
		{	
			Uri mUri = ContentUris.withAppendedId(uri, rowID);
			getContext().getContentResolver().notifyChange(mUri, null);
			return mUri;
			
		}else
		 if (BuildConfig.DEBUG) {
 			Log.e(TITLE_ERROR_EXCEPTION, "Failed to insert new row into table Exception");
 		}
		return null;
	}

	/* (non-Javadoc)
	 * @see android.content.ContentProvider#onCreate()
	 */
	@Override
	public boolean onCreate() {
		Context context = getContext();
        SQLiteConnectionManager dbHelper = new SQLiteConnectionManager(context);
        mSqlDb = dbHelper.getWritableDatabase();
        AssetDatabaseOpenHelper ddAssetDatabaseOpenHelper =new AssetDatabaseOpenHelper(context);
        mSqlDb=ddAssetDatabaseOpenHelper.openDatabase();
        return (mSqlDb == null) ? false : true;
	}

	/* (non-Javadoc)
	 * @see android.content.ContentProvider#query(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String)
	 */
	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
		      String[] selectionArgs, String sortOrder) {
		String tableName = getType(uri);
		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

		queryBuilder.setTables(tableName);
		Cursor cursor = queryBuilder.query(mSqlDb, projection, selection, selectionArgs, null, null, sortOrder);
	    cursor.setNotificationUri(getContext().getContentResolver(), uri);
		return cursor;
	}
	

	/* (non-Javadoc)
	 * @see android.content.ContentProvider#update(android.net.Uri, android.content.ContentValues, java.lang.String, java.lang.String[])
	 */
	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		String tableName = getType(uri);
         return mSqlDb.update(tableName, values, selection,selectionArgs);
          
	}

	
}
